﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;
namespace Littlebeer.Deduplicate
{
    /// <summary>
    /// 查重规则管理器
    /// </summary>
    public static class DeduplicateRulesManager
    {
        /// <summary>
        /// 对查重规则分析并分组
        /// </summary>
        /// <param name="DeduplicateRules">查重规则列表</param>
        /// <returns></returns>
        public static List<DeduplicateRuleGroupItem> GetDeduplicateRuleGroup(List<Deduplicate.Models.DeduplicateRule> DeduplicateRules)
        {
            List<DeduplicateRuleGroupItem> Result = new List<DeduplicateRuleGroupItem>();

            Hashtable fullResult = new Hashtable();

            //All added rule
            List<Deduplicate.Models.DeduplicateRule> AddedDeduplicateRules = new List<Models.DeduplicateRule>();

            //Group Counts
            List<DeduplicateRuleGroupItem> GroupCounts = new List<DeduplicateRuleGroupItem>();

            //View all rules
            foreach (Deduplicate.Models.DeduplicateRule item in DeduplicateRules)
            {
                //view all fileld in each rule
                foreach (var field in item.DeduplicateRuleFields)
                {
                    //if not check the group ,create
                    if (!fullResult.ContainsKey(field.FieldName))
                    {
                        List<Deduplicate.Models.DeduplicateRule> rulelist = new List<Models.DeduplicateRule>();
                        rulelist.Add(item);
                        fullResult.Add(field.FieldName, rulelist);
                    }
                    //if exist,add rule
                    else
                    {
                        List<Deduplicate.Models.DeduplicateRule> Rulelist = fullResult[field.FieldName] as List<Deduplicate.Models.DeduplicateRule>;
                        if (!Rulelist.Contains(item))
                        {
                            Rulelist.Add(item);
                            fullResult[field.FieldName] = Rulelist;
                        }
                    }
                }
            }

            //count group rule count

            foreach (DictionaryEntry group in fullResult)
            {
                List<Deduplicate.Models.DeduplicateRule> rulelist = new List<Models.DeduplicateRule>();
                rulelist = group.Value as List<Deduplicate.Models.DeduplicateRule>;
                GroupCounts.Add(new DeduplicateRuleGroupItem() { GroupName = group.Key.ToString(), Orders = rulelist.Count });
            }

            List<DeduplicateRuleGroupItem> OrderedGroup = GroupCounts.OrderByDescending(d => d.Orders).ToList();
            foreach (DeduplicateRuleGroupItem group in OrderedGroup)
            {
                foreach (Deduplicate.Models.DeduplicateRule rule in fullResult[group.GroupName] as List<Deduplicate.Models.DeduplicateRule>)
                {
                    if (!AddedDeduplicateRules.Contains(rule))
                    {
                        group.DeduplicateRules.Add(rule);
                        AddedDeduplicateRules.Add(rule);
                    }
                }
            }
            return OrderedGroup;
        }
        /// <summary>
        /// 
        /// </summary>
        public class DeduplicateRuleGroupItem
        {
            public string GroupName;
            public List<Deduplicate.Models.DeduplicateRule> DeduplicateRules;
            public int Orders;
            public DeduplicateRuleGroupItem() 
            {
                DeduplicateRules = new List<Models.DeduplicateRule>();
            }
        }
    }
}
